package com.whmall.service.crm.provider;

import cn.hutool.core.collection.CollUtil;
import com.whmall.common.util.BitMapUtils;
import com.whmall.component.entity.dto.CompanyUserQuotationInvervalDTO;
import com.whmall.component.entity.dto.OperatorReqDTO;
import com.whmall.component.provider.AbstractSameProvider;
import com.whmall.component.util.NumberUtils;
import com.whmall.service.crm.api.CompanyUserApi;
import com.whmall.service.crm.entity.dto.company.CompanySyncAddReqDTO;
import com.whmall.service.crm.entity.dto.companyuser.*;
import com.whmall.service.crm.module.companyuser.CompanyUserQuotationInvervalService;
import com.whmall.service.crm.module.companyuser.CompanyUserRepository;
import com.whmall.service.crm.module.companyuser.CompanyUserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * CompanyUserProvider
 *
 * @author alex
 * @since 2020/7/31
 */
@DubboService(interfaceClass = CompanyUserApi.class)
public class CompanyUserProvider extends AbstractSameProvider<CompanyUserService, CompanyUserQueryCondition> implements CompanyUserApi {

    private final CompanyUserService companyUserService;
    private final CompanyUserRepository companyUserRepository;

    @Autowired
    private CompanyUserQuotationInvervalService quotationInvervalService;

    public CompanyUserProvider(CompanyUserService companyUserService,
                               CompanyUserRepository companyUserRepository) {
        this.companyUserService = companyUserService;
        this.companyUserRepository = companyUserRepository;
    }

    @Override
    public Long createCompanyUser(Long requesterId, Long companyId, CreateOrUpdateCompanyUserReqDTO request) {
        return companyUserService.createCompanyUser(requesterId, companyId, request);
    }

    @Override
    public CompanyUserSyncAddRspDTO syncCreateCompanyUser(CompanyUserSyncAddReqDTO reqDTO, Long companyId, OperatorReqDTO operator) {
        return companyUserService.syncCreateCompanyUser(reqDTO,companyId,operator);
    }

    @Override
    public Map<String, CompanyUserSyncAddRspDTO> batchSyncCreateUser(List<CompanyUserSyncAddReqDTO> userList,
                                                                     List<CompanySyncAddReqDTO> companyList,
                                                                     OperatorReqDTO operator) {
        return companyUserService.batchSyncCreateUser(userList,companyList,operator);
    }

    @Override
    public void updateCompanyUser(Long requesterId, Long companyId, Long companyUserId, CreateOrUpdateCompanyUserReqDTO request) {
        companyUserService.updateCompanyUser(requesterId, companyId, companyUserId, request);
        if (CollUtil.isNotEmpty(request.getQuotationList())) {
            request.getQuotationList().forEach(e -> e.setUserId(companyUserId));
            quotationInvervalService.saveOrUpdate(request.getQuotationList(), requesterId);
        }
    }

    @Override
    public void updateEmailNotify(Long userId, List<Integer> notifyList, OperatorReqDTO operatorReqDTO) {
        companyUserService.updateEmailNotify(userId, notifyList, operatorReqDTO);
    }

    @Override
    public void lockBuy(Long requesterId, Long companyId, Long companyUserId, String lockReason) {
        companyUserService.lockBuy(requesterId, companyId, companyUserId, lockReason);
    }

    @Override
    public void unlockBuy(Long requesterId, Long companyId, Long companyUserId) {
        companyUserService.unlockBuy(requesterId, companyId, companyUserId);
    }

    @Override
    public void lockSale(Long requesterId, Long companyId, Long companyUserId, String lockReason) {
        companyUserService.lockSale(requesterId, companyId, companyUserId, lockReason);
    }

    @Override
    public void unlockSale(Long requesterId, Long companyId, Long companyUserId) {
        companyUserService.unlockSale(requesterId, companyId, companyUserId);
    }

    @Override
    public void setDefaultUser(Long requesterId, Long companyId, Long companyUserId) {
        companyUserService.setDefaultUser(requesterId, companyId, companyUserId);
    }

    @Override
    public CompanyUserDetailsRspDTO getDetailsById(Long companyId, Long companyUserId) {
        CompanyUserDetailsRspDTO companyUserDetailsRspDTO = companyUserRepository
                .getDetailsById(companyId, companyUserId);
        companyUserDetailsRspDTO.setQuotationInvervalRspDTOS(
                quotationInvervalService.listByUserId(companyUserId)
                        .stream().map(e -> {
                            CompanyUserQuotationInvervalRspDTO interval = new CompanyUserQuotationInvervalRspDTO();
                            BeanUtils.copyProperties(e, interval);
                            interval.setStartPrice(NumberUtils.divide100(e.getStartPrice()));
                            interval.setEndPrice(NumberUtils.divide100(e.getEndPrice()));
                            return interval;
                        }).collect(Collectors.toList())
        );
        companyUserDetailsRspDTO.setNotifySettingsDTO(NotifySettingsDTO.builder()
                .email(companyUserDetailsRspDTO.getEmail())
                .isVerifiedEmail(companyUserDetailsRspDTO.getIsVerifiedEmail())
                .notifyList(BitMapUtils.toList(companyUserDetailsRspDTO.getNotifyBitmap(), BitMapUtils.EMAIL_NOTIFY_LENGTH))
                .build());
        return companyUserDetailsRspDTO;
    }

    @Override
    public List<CompanyUserListItemRspDTO> listUserByCompany(Long companyId) {
        return companyUserRepository.listUserByCompany(companyId);
    }

    @Override
    public List<CompanyUserKeyFieldRspDTO> getAvailableCompanyUsers(Long companyId) {
        return companyUserRepository.getAvailableCompanyUsers(companyId);
    }

    @Override
    public List<CompanyUserListByUsernameRspDTO> listByUsername(String username, Integer rule, Integer limitNum) {
        return companyUserService.listByUsername(username, rule, limitNum);
    }

    @Override
    public CompanyUserKeyFieldRspDTO findById(Long id) {
        return companyUserService.findById(id);
    }

    @Override
    public Map<Long, CompanyUserKeyFieldRspDTO> listByIds(List<Long> ids) {
        return companyUserService.listByIds(ids);
    }

    @Override
    public Map<Long, CompanyUserKeyFieldRspDTO> listByIds(CompanyUserQueryReqDTO reqDTO) {
        return companyUserService.listByIds(reqDTO);
    }

    @Override
    public Map<String, CompanyUserKeyFieldRspDTO> queryByOneField(CompanyUserQueryByOneFieldReqDTO reqDTO) {
        return companyUserService.queryByOneField(reqDTO);
    }

    @Override
    public List<CompanyUserQuotationInvervalDTO> listQuotationInterval(Long id) {
        return quotationInvervalService.listByUserId(id);
    }

    @Override
    public Map<Long, List<CompanyUserQuotationInvervalDTO>> queryUserQuotationIntervalMap(List<Long> userIdList) {
        return quotationInvervalService.queryUserQuotationIntervalMap(userIdList);
    }

    @Override
    public List<CompanyUserIndexRspDTO> listByCompanyIds(List<Long> companyIds) {
        return companyUserService.listByCompanyIds(companyIds);
    }

    @Override
    public List<CompanyUserKeyFieldRspDTO> listValidEmailByIds(Integer notifyBitmap, List<Long> ids) {
        return companyUserService.listValidEmailByIds(notifyBitmap, ids);
    }

    @Override
    public void updateIsSupplierCustomer(CompanyUserUpdateIsSupplierCustomerReqDTO reqDTO) {
        companyUserService.updateIsSupplierCustomer(reqDTO);
    }

    @Override
    public CompanyUserKeyFieldRspDTO authentication(CompanyUserLoginReqDTO reqDTO) {
        return companyUserService.authentication(reqDTO);
    }

    @Override
    public void resetPassword(CompanyUserResetPasswordReqDTO reqDTO) {
        companyUserService.resetPassword(reqDTO);
    }

    @Override
    public void bind(CompanyUserBindReqDTO reqDTO) {
        companyUserService.bind(reqDTO);
    }

    @Override
    public Long register(CompanyUserRegisterReqDTO reqDTO) {
        return companyUserService.register(reqDTO);
    }

    @Override
    public CompanyUserNewUserApplyDTO newUserApply(CompanyUserNewUserApplyDTO reqDTO) {
        return companyUserService.newUserApply(reqDTO);
    }

    @Override
    public CompanyUserSupplierApplyDTO supplierApply(CompanyUserSupplierApplyDTO reqDTO) {
        return companyUserService.supplierApply(reqDTO);
    }

    @Override
    public void fillData(CompanyUserFillDataReqDTO reqDTO) {
        companyUserService.fillData(reqDTO);
    }

    @Override
    public CompanyUserAccountUpdateApplyDTO accountUpdateApply(CompanyUserAccountUpdateApplyDTO reqDTO) {
        return companyUserService.accountUpdateApply(reqDTO);
    }

    @Override
    public Integer updateEmailById(Long id, String email) {
        return companyUserService.updateEmailById(id, email);
    }

    @Override
    public void updateInvoiceEmailById(Long id, String email) {
         companyUserService.updateInvoiceEmailById(id, email);
    }

    @Override
    public Long findIdByEmail(String email) {
        return companyUserService.findIdByEmail(email);
    }

    @Override
    public CompanyUserDTO infoById(Long id, CompanyUserOptionDTO option) {
        return companyUserService.infoById(id, option);
    }

    @Override
    public List<CompanyUserListInfoRspDTO> listInfoBy(CompanyUserListInfoQueryReqDTO queryReqDTO) {
        return companyUserService.listInfoBy(queryReqDTO);
    }

    @Override
    public CompanyUserKeyFieldRspDTO findByEmail(String email) {
        return companyUserService.findByEmail(email);
    }

    @Override
    public CompanyUserKeyFieldRspDTO findByPhoneOrEmail(String phoneOrEmail) {
        return companyUserService.findByPhoneOrEmail(phoneOrEmail);
    }

    @Override
    public List<Long> selectByIdsWithShareLocked(List<Long> ids) {
        return companyUserService.selectByIdsWithShareLocked(ids);
    }

    @Override
    public void reVerified(Long id, OperatorReqDTO operatorReqDTO) {
        companyUserService.reVerified(id, operatorReqDTO);
    }
}
